<?php
/**
 * Library Of Shared Code (LOSC)
 *
 * LICENSE
 *
 * This source file is subject to the New BSD License that is bundled
 * with this package in the file LICENSE.txt.
 *
 * @category   LOSC Framework
 * @package    View
 * @subpackage Helper
 * @copyright  Copyright (c) 2008 Robin Skoglund (http://robinsk.net/)
 * @license    http://creativecommons.org/licenses/BSD/  New BSD License
 */

/**
 * View helper for a userbox (You are logged in as ...)
 *
 * @category   LOSC Framework
 * @package    View
 * @subpackage Helper
 * @copyright  Copyright (c) 2008 Robin Skoglund (http://robinsk.net/)
 * @license    http://creativecommons.org/licenses/BSD/  New BSD License
 */
class Losc_View_Helper_Userbox extends Zym_View_Helper_Html_Abstract
{
    /**
     * Whether a link for registering should be included
     *
     * @var boolean
     */
    private $_enableRegister = true;
    
    /**
     * Returns helper instance
     * 
     * @return Losc_View_Helper_Userbox
     */
    public function userbox($enableRegister = null)
    {
        if ($enableRegister !== null) {
            $this->_enableRegister = (bool) $enableRegister;
        }
        
        return $this;
    }
    
    /**
     * Renders userbox
     * 
     * @return string
     */
    public function toString($indent = null)
    {
        $indent = (null !== $indent)
                ? $this->_getWhitespace($indent)
                : $this->getIndent();
        
        $view = $this->getView();
        
        $items = array();
        
        // check if user is logged in
        if ($id = Zend_Auth::getInstance()->getIdentity()) {
            $user = App_User::getCurrentUser();
            
            $signoutUrl = $view->url(array(), 'logout', true);
            $accountUrl = $view->url(array(
                'module' => 'user',
                'controller' => 'settings',
                'action' => 'index'
            ), 'default', true);
            
            //$items[] = $view->translate('Signed in as') . ' ' . $id->username;
            //$items[] = '<span title="' . $view->translate('Signed in as %s', $id->username) . '">' . $id->username . '</span>';
            $items[] = '<b>' . $user->username . '</b>';
            $items[] = '<a title="' . $view->translate('Your settings') . '" href="' . $accountUrl . '">' . $view->translate('Settings') . '</a>';
            $items[] = '<a href="' . $signoutUrl . '">' . $view->translate('Sign out') . '</a>';
        } else {
            $signinUrl = $view->url(array(), 'login', true);
            
            $items[] = $view->translate('Not signed in');
            $items[] = '<a href="' . $signinUrl . '">' . $view->translate('Sign in') . '</a>';
            
            if ($this->_enableRegister) {
                $registerUrl = $view->url(array(
                    'module' => 'user',
                    'controller' => 'register',
                    'action' => 'index'
                ), 'default', true);
                
                $items[] = '<a href="' . $registerUrl . '">' . $view->translate('Register account') . '</a>';
            }
        }
        
        $first = true;
        $html = "$indent<ul class=\"userbox\">\n";
        foreach ($items as $item) {
            if ($first) {
                $html .= "$indent    <li class=\"first\">$item</li>\n";
                $first = false;
            } else {
                $html .= "$indent    <li>$item</li>\n";
            }
        }
        $html .= "$indent</ul>\n";
        
        return $html;
    }
    
    /**
     * Magic method: Renders userbox
     * 
     * @return string
     */
    public function __toString()
    {
        return $this->toString();
    }
}
